﻿using System;

namespace KinonekoSoftware.Studio.ViewModels
{
    
#if WINDOWS
    
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using KinonekoSoftware.CodariaDB.Presets;
    
    using KinonekoSoftware.Foundation.UI.Media;
    
    public partial class SettingViewModel
    {
        private ImageSource _avatar;
        private ImageSource _icon;


        public ImageSource Icon
        {
            get => _icon;
            set => SetValue(ref _icon, value);
        }
        
        public ImageSource Avatar
        {
            get => _avatar;
            set => SetValue(ref _avatar, value);
        }
        
        public BitmapImage GetIcon(Preset_Icon icon)
        {
            BitmapImage bi;
            byte[]      buffer;

            if (icon == Preset_Icon.Override)
            {
                buffer = File.ReadAllBytes(IconSystem.GetPresetIconFilePath(_repository.Workspace));
                bi     = MediaFactory.ToBitmap(buffer);
            }
            else if (_PresetIcon.TryGetValue(icon, out var v) && v is BitmapImage v2)
            {
                return v2;
            }
            else
            {
                buffer = IconSystem.GetPresetIcon(icon);
                bi     = MediaFactory.ToBitmap(buffer);
                _PresetIcon.Add(icon, bi);
            }
            
            return bi;
        }
        
        public BitmapImage GetAvatar(Preset_Avatar icon)
        {
            BitmapImage bi;
            byte[]      buffer;

            if (icon == Preset_Avatar.Override)
            {
                buffer = File.ReadAllBytes(GravatarSystem.GetPresetAvatarFilePath(_repository.Workspace));
                bi     = MediaFactory.ToBitmap(buffer);
            }
            else if (_PresetAvatar.TryGetValue(icon, out var v) && v is BitmapImage v2)
            {
                return v2;
            }
            else
            {
                buffer = GravatarSystem.GetPresetAvatar(icon);
                bi     = MediaFactory.ToBitmap(buffer);
                _PresetAvatar.Add(icon, bi);
            }
            
            return bi;
        }
    }
#endif
}